<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class MealCard extends Model
{
    const CREATED_AT = 'insert_time';
    const UPDATED_AT = 'update_time';

    /**
     * 与模型关联的数据表。
     *
     * @var string
     */
    protected $table = 'lexiang_meal_card';

    protected $primaryKey = 'card_id';

    public $timestamps = false;
    /**
     * 可以被批量赋值的属性。
     *
     * @var array
     */
    protected $fillable = [
    	'type','num',
    	'start_time','stop_time',
    	'day_once','meal_max_price',
    	'card_activity_price','card_price',
    	'card_sales','card_status',
    	'insert_time','update_time',
    	'card_img','card_name','card_desc',
    	'card_frequency','card_param','range_id','delivery_free'
    ];

    public function editAttr($data,$id)
    {
        $nowtime = time();
        $card = self::find($id);
        $card->update_time = $nowtime;
        foreach ($data as $key => $value) {
            $card->$key = $value;
        }
        return $card->save();
    }

    public function getRangeTextAttribute($v)
    {
        if(empty($this->range_id)){
            return '全范围';
        }
        $ids = explode(',',$this->range_id);
        $names = Circle::whereIn('circle_id',$ids)->pluck('circle_name')->toarray();
        return $names;
    }

    public function getCardImgAttribute($v){
        return 'https://jiashilexiang.oss-cn-beijing.aliyuncs.com/MealCardImgs'.$v;
    }


}
